<template>
  <PageWrapper>
    <BasicTable
      class="page-search-table"
      @register="registerTable"
      :row-selection="{ onChange: doSelection }"
    >
      <template #tableTitle>
        <a-button type="primary" @click="openAdd">新增</a-button>
        <a-button type="danger" @click="openDelMulit">删除</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            // {
            //   icon: 'mdi:apple-icloud',
            //   tooltip: '云状态',
            //   onClick: openCloudStatus.bind(null, record),
            // },
            // {
            //   label: '查看',
            //   onClick: goSee.bind(null, record),
            // },
            {
              label: '编辑',
              onClick: openEdit.bind(null, record),
            },
            {
              label: '删除',
              popConfirm: {
                title: `确认删除设备:${record.name}吗？`,
                confirm: openDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref, onActivated, createVNode } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { getData, tableConfig } from './tableData';
  import { DelMainEquipmentList } from '/@/api/sys/gateway/gateway';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { Modal } from 'ant-design-vue';

  export default defineComponent({
    name: 'EquipmentList',
    components: {
      PageWrapper,
      BasicTable,
      TableAction,
    },
    emits: ['register'],
    setup(_) {
      const [registerTable, { reload, getForm }] = useTable(tableConfig);
      const { createMessage } = useMessage();
      const loading = ref(false);
      const router = useRouter();
      //跳转查看
      const goSee = (rowdata) => {
        if (rowdata.tag_group.length > 0) {
          let tag_list: string[] = [];
          rowdata.tag_group.map((item: any) => {
            tag_list.push(item.tag_name);
          });
          rowdata.tag_list = tag_list;
        }
        localStorage.setItem('deviceinfo', JSON.stringify(rowdata));
        router.push('/device/subDetail');
      };
      //删除操作
      async function openDelete(rowData) {
        let dellist: Number[] = [];
        if (Array.isArray(rowData)) {
          dellist = rowData;
        } else {
          dellist.push(rowData.id);
        }
        await DelMainEquipmentList({
          id_list: dellist,
        });
        createMessage.success('删除成功');
        reload();
      }
      function openAdd() {
        router.push('/device/mainDetail?type=add');
      }
      function openEdit(rowdata: Recordable) {
        router.push({
          path: '/device/mainDetail',
          query: {
            id: rowdata.id,
            type: 'edite',
          },
        });
      }
      //多选删除
      function openDelMulit() {
        if (delArray.length == 0) {
          createMessage.warn('请选择删除项');
          return;
        }
        Modal.confirm({
          title: () => `确定删除 ${delArray.length}项吗 ?`,
          icon: () => createVNode(ExclamationCircleOutlined),
          okType: 'danger',
          onOk() {
            openDelete(delArray);
          },
        });
      }
      //多选中
      let delArray: number[] = reactive([]);
      function doSelection(selectedRowKeys: []) {
        delArray = [...selectedRowKeys];
      }
      onActivated(() => {
        const status = router.currentRoute.value.params.status;
        if (status) {
          getForm().setFieldsValue({
            device_status: parseInt(status.toString()),
          });
          setTimeout(() => {
            reload();
          }, 0);
        } else {
          reload();
        }
      });
      return {
        goSee,
        tableData: getData,
        registerTable,
        openAdd,
        openEdit,
        openDelete,
        openDelMulit,
        doSelection,
        loading,
      };
    },
  });
</script>
